package com.ruoyi.canteen.mapper;

import java.util.List;
import com.ruoyi.canteen.domain.CanteenCpSpecial;
import org.apache.ibatis.annotations.*;

/**
 * 菜品规格Mapper接口
 * 
 * @author ruoyi
 * @date 2021-09-10
 */
public interface CanteenCpSpecialMapper 
{
    /**
     * 查询菜品规格
     * 
     * @param id 菜品规格主键
     * @return 菜品规格
     */
    public CanteenCpSpecial selectCanteenCpSpecialById(Long id);

    /**
     * 查询菜品规格列表
     * 
     * @param canteenCpSpecial 菜品规格
     * @return 菜品规格集合
     */
    public List<CanteenCpSpecial> selectCanteenCpSpecialList(CanteenCpSpecial canteenCpSpecial);

    /**
     * 新增菜品规格
     * 
     * @param canteenCpSpecial 菜品规格
     * @return 结果
     */
    public int insertCanteenCpSpecial(CanteenCpSpecial canteenCpSpecial);

    /**
     * 修改菜品规格
     * 
     * @param canteenCpSpecial 菜品规格
     * @return 结果
     */
    public int updateCanteenCpSpecial(CanteenCpSpecial canteenCpSpecial);

    /**
     * 删除菜品规格
     * 
     * @param id 菜品规格主键
     * @return 结果
     */
    public int deleteCanteenCpSpecialById(Long id);

    /**
     * 批量删除菜品规格
     * 
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteCanteenCpSpecialByIds(Long[] ids);

    /**
     * 查询菜品规格列表
     *
     * @param cpId 菜品规格
     * @return 菜品规格
     */
    @Select("SELECT id,cp_id,cp_specs,price,stock from canteen_cp_special where cp_id = #{cpId} order by price asc")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "cpId", column = "cp_id"),
            @Result(property = "cpSpecs", column = "cp_specs"),
            @Result(property = "price", column = "price"),
            @Result(property = "stock", column = "stock"),
    })
    public List<CanteenCpSpecial> selectCanteenCpSpecialByCpId(Integer cpId);

    /**
     * 根据菜品id + 扣减数量
     * 更新：菜品库存数量
     * @param cpId 菜品id
     * @param amount 扣减数量
     * @return
     */
    @Update("update canteen_cp_special s " +
            "set s.stock = s.stock-#{amount} " +
            "where s.cp_id=#{cpId} and s.stock > #{amount}")
    public int reduceCpStock(@Param("cpId") Long cpId, @Param("amount") Integer amount);

    /**
     * 根据菜品id + 扣减数量
     * 更新：菜品库存数量
     * @param cpSpecialId 菜品规格id
     * @param amount 扣减数量
     * @return
     */
    @Update("update canteen_cp_special s " +
            "set s.stock = s.stock-#{amount} " +
            "where s.id=#{cpSpecialId} and s.stock > #{amount}")
    public int reduceCpSpecialStock(@Param("cpSpecialId") Long cpSpecialId, @Param("amount") Integer amount);

}
